package com.ruoyi.house.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.house.domain.TbAddress;
import com.ruoyi.house.service.ITbAddressService;

import javax.servlet.http.HttpServletResponse;

/**
 * 地区Controller
 * 
 * @author ruoyi
 * @date 2024-10-09
 */
@RestController
@RequestMapping("/house/address")
public class TbAddressController extends BaseController
{
    @Autowired
    private ITbAddressService tbAddressService;

    /**
     * 查询地区列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TbAddress tbAddress)
    {
        startPage();
        List<TbAddress> list = tbAddressService.selectTbAddressList(tbAddress);
        return getDataTable(list);
    }

    /**
     * 根据父ID查询地址列表
     */
    @GetMapping("/listByPid")
    public AjaxResult listByPid(@RequestParam(required = false) Integer pid)
    {
        List<TbAddress> list = tbAddressService.selectTbAddressListByPid(pid);
        return AjaxResult.success(list);
    }

    /**
     * 导出地区列表
     */
    @Log(title = "地区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAddress tbAddress)
    {
        List<TbAddress> list = tbAddressService.selectTbAddressList(tbAddress);
        ExcelUtil<TbAddress> util = new ExcelUtil<TbAddress>(TbAddress.class);
        util.exportExcel(response, list, "地区数据");
    }

    /**
     * 获取地区详细信息
     */
    @GetMapping(value = "/{addressId}")
    public AjaxResult getInfo(@PathVariable("addressId") Integer addressId)
    {
        return AjaxResult.success(tbAddressService.selectTbAddressByAddressId(addressId));
    }

    /**
     * 新增地区
     */
    @Log(title = "地区", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbAddress tbAddress)
    {
        return toAjax(tbAddressService.insertTbAddress(tbAddress));
    }

    /**
     * 修改地区
     */
    @Log(title = "地区", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbAddress tbAddress)
    {
        return toAjax(tbAddressService.updateTbAddress(tbAddress));
    }

    /**
     * 删除地区
     */
    @Log(title = "地区", businessType = BusinessType.DELETE)
	@DeleteMapping("/{addressIds}")
    public AjaxResult remove(@PathVariable Integer[] addressIds)
    {
        return toAjax(tbAddressService.deleteTbAddressByAddressIds(addressIds));
    }
}